import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome.vue')
const Users = () => import(/* webpackChunkName: "users_roles_rigths" */ '../components/user/Users.vue')
const Roles = () => import(/* webpackChunkName: "users_roles_rigths" */ '../components/power/Role.vue')
const Rights = () => import(/* webpackChunkName: "users_roles_rigths" */ '../components/power/Rights.vue')
const Cate = () => import(/* webpackChunkName: "cate_params" */ '../components/good/Cate.vue')
const Params = () => import(/* webpackChunkName: "cate_params" */ '../components/good/Params.vue')
const Good = () => import(/* webpackChunkName: "cate_params" */ '../components/good/List.vue')
const Add = () => import(/* webpackChunkName: "cate_params" */ '../components/good/Add.vue')
const Order = () => import(/* webpackChunkName: "cate_params" */ '../components/order/Order.vue')
const Report = () => import(/* webpackChunkName: "cate_params" */ '../components/report/Report.vue')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/welcome',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: Home,
    children: [
      { path: '/welcome', name: 'Welcome', component: Welcome },
      { path: '/users', name: 'Users', component: Users },
      { path: '/roles', name: 'Roles', component: Roles },
      { path: '/rights', name: 'Rights', component: Rights },
      { path: '/categories', name: 'Cate', component: Cate },
      { path: '/params', name: 'Params', component: Params },
      { path: '/goods', name: 'Good', component: Good },
      { path: '/add', name: 'Add', component: Add },
      { path: '/orders', name: 'Order', component: Order },
      { path: '/reports', name: 'Report', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
